MAIN.controller('appropriationCtrl', ["$scope", "$rootScope", "tools", function ($scope, $rootScope, tools) {
    $("#appropriations-map").height($(document).height());
    var map = new BMap.Map("appropriations-map", {mapType: BMAP_HYBRID_MAP});
    if (tools.town() == '唐河') {
        var point = new BMap.Point(112.8138750000, 32.6874880000);
    } else {
        var point = new BMap.Point(112.9544230000, 33.0621780000);
    }
    map.centerAndZoom(point, 13);
    map.enableScrollWheelZoom();

    var salers = [];
    var points;
    tools.get({
        url: '/Intelligence-Business-Management/getNicks.htm',
        data: {
            uuid: '69341649-c33d-4fd3-b6d1-7d264df7c8b0'
        },
        succ: function (resp) {
            if (resp.success) {
                angular.forEach(resp.value, function (saler, index) {
                    saler.push(99 + Number(Math.random().toFixed(2)));
                });
                salers = resp.value;
                points = tools.get_lat_lng_bound(map, salers);
                console.log(points);
                showPoints();
            }
        }
    });

    function showPoints() {
        for (var i = 0; i < points.length; i++) {
            (function (i) {
                var pt = new BMap.Point(points[i].lng, points[i].lat);
                var myIcon = new BMap.Icon("../images/" + points[i].platform + ".jpg", new BMap.Size(30, 30));
                var marker = new BMap.Marker(pt, {icon: myIcon});

                marker.addEventListener("click", function () {
                    var sContent = "<h3 style = 'font-size: 20px;margin-bottom: 10px;'></h3>" +
                        "<p style='margin-bottom: 10px;'>近30天销售额: " + points[i].count + "元</p>" +
                        "<p style='margin-bottom: 10px;'>近30天销售量: " + points[i].saleCount + "</p>" +
                        "<p style='margin-bottom: 10px;'>客单价: " + points[i].price + "元</p>" +
                        "<p style='margin-bottom: 10px;'>好评率: " + points[i].feedbackRate + "</p>" +
                        "<div style='margin-bottom:10px;'>联系旺旺: " + points[i].name + "</div>" +
                        "<input type='text' style='height: 25px;line-height: 25px;'/><button style = 'padding:6px 10px;margin-left:10px;'>发送</button>"

                    var infoWindow = new BMap.InfoWindow(sContent);
                    this.openInfoWindow(infoWindow);
                });
                var point = new BMap.Point(points[i].lng, points[i].lat);
                var opts = {
                    position: point,
                    offset: new BMap.Size(15, -30)
                };
                var label = new BMap.Label(points[i].count + '元', opts);
                map.addOverlay(marker);
                map.addOverlay(label);
                points[i].marker = marker;
                points[i].label = label;
            })(i);
        }
    }


    heatmapOverlay = new BMapLib.HeatmapOverlay({"radius": 20});
    map.addOverlay(heatmapOverlay);

    $scope.platform = {
        tb: true,
        tm: true,
        jd: true
    }

    $scope.$watch('platform.tb', function (newV, oldV) {
        angular.forEach(points, function (point) {
            if (point.platform == 'tb') {
                if (newV) {
                    point.marker.show();
                    point.label.show();
                } else {
                    point.marker.hide();
                    point.label.hide();
                }
            }
        });
    });
    $scope.$watch('platform.tm', function (newV, oldV) {
        angular.forEach(points, function (point) {
            if (point.platform == 'tm') {
                if (newV) {
                    point.marker.show();
                    point.label.show();
                } else {
                    point.marker.hide();
                    point.label.hide();
                }
            }
        });
    });
    $scope.$watch('platform.jd', function (newV, oldV) {
        angular.forEach(points, function (point) {
            if (point.platform == 'jd') {
                if (newV) {
                    point.marker.show();
                    point.label.show();
                } else {
                    point.marker.hide();
                    point.label.hide();
                }
            }
        });
    });
}]);



